<?php

/**
 * @category   mediad2
 * @package    Mediad2_Service
 */
class Mediad2_Service_Admin_Permission extends Mediad2_Service_Abstract
{
	/**
	 * 初始化
	 */
	public function init()
	{
		$this->setMapper('Mediad2_Mapper_Permission');
		$this->setModel('Mediad2_Model_Permission');
	}
	
	/**
	 * 返回符合条件的ACL资源
	 *
	 * @param integer $pageIndex
	 * @param integer $pageNum
	 * @param string $where
	 * @param string $order
	 * @param boolean $returnArray
	 * @return array Zend_Acl_Resource
	 */
	public function fetchAclResource($pageIndex = 1, $pageNum = 20, $where = null, $order = null, $returnArray = false)
	{
		$mapper = $this->getMapper();
		$dao = $mapper->getDao();
		$selector = $dao->select();
		if(strlen($where)>0)
			$selector->where($where);
		if(strlen($order)>0)
			$selector->order($order);
		$selector->limit($pageNum,($pageIndex -1) * $pageNum);
	
		//定义检索字段
		$cols = array('moduleName', 'controller');
		$selector->from($selector->getTable(),$cols);
		//增加分组条件
		$selector->group($cols);
		
		$rows = $dao->fetchAll($selector);
		$result = array();
		foreach ($rows as $key => $value) {
			$result[] = strtolower($value->moduleName) . '_' . strtolower($value->controller);
		}
		
		return $result;
	}
	
	/**
	 * 批量删除指定的权限记录
	 *
	 * @param array of $PermissionId
	 * @return boolean return true if no error.
	 */
	public function deleteBatch(array $arrayPermissionId)
	{
		if ( count($arrayPermissionId) == 0 ) {
			return false;
		}
	
		//构造where条件
		$where = 'permissionId in ( ' . implode(",", $arrayPermissionId) . ')';
	
		$mapper = $this->getMapper();
		$dao = $mapper->getDao();
		$result = $dao->delete($where);
		if ($result > 0) {
			return true;
		} else {
			return false;
		}
	}
	
}